[TOC]

Double Type

Most of the numeric operations and functions involve double values. There is no integer type, hence all integers are stored as doubles in the memory. For example, when 1 is entered, it is stored as a double:

Input
1
Output
ans = 
 1.0000

A negative integer, say, -200, is also stored as a double:

Input
200
Output
ans = 
 200.00

Of course, decimal numbers are stored as doubles:

Input
0.34
Output
ans = 1e-1 × 
 3.4000

A nice thing about using doubles to represent integers is that you don't have to worry about overflow. (A downside is that it uses more system memory.) As you may know, there is a hard limit on an integer's storable magnitude. If this limited is exceeded, error occurs. Whereas, if a double's magnitude is too large, it will be represented as infinity, i.e., Inf, and no error will be thrown.

In the following example, two numbers, and , are entered. The former is so large that it is represented as infinity Inf, whereas the latter is well within the range and hence is represented as a finite number. In the second line, a(1) + a(2) is performed and the result is Inf as expected.

Input
a = [2e308, 1e308]
a(1) + a(2)
Output
a = 1e308 × 
 Inf      1.0000

ans = 
 Inf   

Not-a-number NaN

When a double has an undefined value, it is stored as NaN, known as not-a-number. The following expressions are undefined and hence are evaluated to NaN:

Input
tan(inf)
inf - inf
Output
ans = 
 NaN   

ans = 
 NaN   

As expected, computation involving NaN gives NaN as shown in the examples below. In the last example, cummax computes the cumulative maxima. That means, it obtains the maxima of the vectors [1], [1, 2], [1, 2, NaN] and [1, 2, NaN, 4]. The maxima of the last two vectors are undefined.

Input
sin(NaN)
NaN + 2
1 / NaN
abs(NaN)
cummax([1 2 NaN 4], 'includenan')
Output
ans = 
 NaN   

ans = 
 NaN   

ans = 
 NaN   

ans = 
 NaN   

ans = 
 1.0000   2.0000   NaN      NaN 

Exponential Form

A double can be created by the exponential operator. For examples, 2e-3 and 0.1e10 mean and , respectively. It must be preceded by a numeric character. For example, e-3 is invalid since it is confused with the variable e subtracted by 1.

The exponential form is useful when the number has many zeros (either very large or very small). You may be aware that SIMO uses the exponential form in the console when outputs are printed out. This makes the outputs tidy and saves some space on the screen.

Input
rand(5)* 0.0001
rand(5)* 10000
Output

In the output below, 1e-5 means , whereas 1e5 means . You can also use 1e+5 for .

ans = 1e-5 × 
 4.5669   8.7742   2.4968   0.3991   3.6013
 0.2391   0.9338   3.9110   1.5162   2.3307
 4.8043   4.0122   7.7958   0.2360   9.4718
 3.6369   1.2876   5.3345   0.7325   1.0268
 9.4136   9.2986   3.2664   9.0404   8.5540

ans = 1e5 × 
 3.7354   2.7741   4.9024   0.3723   8.6278
 3.8693   2.2124   8.5168   6.6556   9.9766
 6.6533   9.9324   6.8080   4.5457   1.4771
 7.0725   0.7308   9.4821   3.3132   5.4889
 6.2265   6.2393   3.9497   1.4531   8.0690

More examples can be found below:

Input
12e4
12e-4
1 / 2000
200000 * pi
Output
ans = 1e5 × 
 1.2000

ans = 1e-3 × 
 1.2000

ans = 1e-4 × 
 5.0000

ans = 1e5 × 
 6.2832